<?php
namespace modules\auth;
use \modules\ui;
use \phiction\array_utils as arr;

class page
{
    static function mapping()
    {
        return [
            '/login' => 'login',
            '/denied' => 'denied',
            '/register' => 'register',
        ];
    }

    static function login($q, $html, &$args)
    {
        $redirect_refresh = arr::get_or($args, 'REDIRECT-REFRESH', false);
        $redirect = $redirect_refresh ? null : '/';

        $message = null;
        if (arr::get($args, 'MESSAGE', $msg))
            $message = ['div', 'class' => 'error-message', $msg];

        if (is_null($message)) {
            $args['HEADER-CONTROL'] = ui::header_control($html, [
                [ui::icon_label('注册', 'user-plus'), '/auth/register'],
            ]);
        }

        $form = ui::form($html, '/api/auth/login',
            ui::icon_label('登录', 'sign-in'), [
                ['text', '组织代号', 'name' => 'codename', 'maxlength' => 64],
                ['text', '用户名', 'name' => 'username', 'maxlength' => 64],
                ['password', '密码', 'name' => 'password', 'maxlength' => 64],
            ], [
                [ui::icon_label('返回首页', 'angle-left'), '/'],
            ], $redirect);

        $args['SUBTITLE'] = '登录';

        return ui::card(['.',
            $message,
            $form,
        ], "登录");
    }

    static function denied($q, $html, &$args)
    {
        $message = arr::get_or($args, 'MESSAGE', '权限不足') . "：请尝试使用有权限的账号登录";
        $args['MESSAGE'] = $message;
        $args['REDIRECT-REFRESH'] = true;
        return self::login($q, $html, $args);
    }

    static function register($q, $html, &$args)
    {
        $args['HEADER-CONTROL'] = ui::header_control($html, [
            [ui::icon_label('登录', 'sign-in'), '/auth/login'],
        ]);

        $form = ui::form($html, '/api/auth/register',
            ui::icon_label('注册', 'user-plus'), [
                ['text', '组织名称', 'name' => 'orgname', 'maxlength' => 64],
                ['text', '组织代号', 'name' => 'codename', 'maxlength' => 64],
                ['text', '管理员姓名', 'name' => 'name', 'maxlength' => 64],
                ['text', '管理员用户名', 'name' => 'username', 'maxlength' => 64],
                ['password', '管理员密码', 'name' => 'password', 'maxlength' => 64],
                ['password', '再次输入密码', 'data-same-value' => 'password', 'maxlength' => 64],
            ], [
                [ui::icon_label('返回首页', 'angle-left'), '/'],
            ], '/');

        $args['SUBTITLE'] = "注册";
        return ui::card(['.',
            ['p', '* 免费注册，无限使用。正式版之后不删号。'],
            $form,
        ], "注册");
    }
}

